//
//  AppLauncher.swift
//  SwiftTools
//
//  Created by Samuel Richard Magny on 15-10-03.
//  Copyright © 2015 Samuel Richard Magny. All rights reserved.
//

import Cocoa

public class AppLauncher: NSObject {

    var task:NSTask = NSTask()
    var launchPath:String = ""
    public var args:Array<String> = []
    
    public func launch()
    {
        task.launchPath = self.launchPath
        task.arguments = self.args
        
        task.launch()
    }
    
    public func launch(terminationHandler:(withSuccess:Bool) -> ())
    {
        task.terminationHandler = {(task) -> Void in
            let status = self.task.terminationStatus
            var commandSuccess:Bool = true;
            if(status != 0) {
                commandSuccess = false
            }
            
            terminationHandler(withSuccess: commandSuccess)
        }
        
        launch()
    }
    
    public func terminate()
    {
        if task.running {
            task.terminate()
        }
    }

}
